package com.shengzhe.ostudent.dao.impl;

import com.shengzhe.ostudent.common.exception.OsException;
import com.shengzhe.ostudent.dao.RoleDao;
import com.shengzhe.ostudent.entity.PageInfo;
import com.shengzhe.ostudent.entity.Role;
import com.shengzhe.ostudent.entity.RoleCondition;
import com.shengzhe.ostudent.utils.OsBeanUtils;
import com.shengzhe.ostudent.utils.jdbc.DaoUtils;
import com.shengzhe.ostudent.utils.jdbc.JDBCUtils;
import com.shengzhe.ostudent.utils.jdbc.WhereSql;
import com.shengzhe.ostudent.utils.result.E;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class RoleDaoImpl implements RoleDao {
    @Override
    public int add(Role role) {
        String sql = "insert into role (name,parentId,level,createTime,updateTime) values(?,?,?,?,?)";
        Date date = new Date();
        System.out.println(sql);
        JDBCUtils.executeUpdate(sql, role.getName(), role.getParentId(), role.getLevel(), date, date);
        role.setName(role.getName() + "xxx");
        return JDBCUtils.executeUpdate(sql, role.getName(), role.getParentId(), role.getLevel(), date, date);
    }

    @Override
    public List<Role> list() {
        String sql = "select * from role";
        ResultSet rs = JDBCUtils.executeQuery(sql);
        return OsBeanUtils.resultSetToList(rs, Role.class);
    }

    @Override
    public Role findById(long id) throws SQLException {
        String sql = JDBCUtils.getSql("role.list");
        ResultSet rs = JDBCUtils.executeQuery(sql);
        if (!rs.next()) {
            throw new OsException(E.USER_CODE_N50);
        }
        rs.previous();
        return OsBeanUtils.resultSetToBean(rs, Role.class);
    }

    /**
     * 分页显示查询的role列表
     * RoleCondition封装了我们的查询条件
     * WhereSql类是专门用来构造where子句的
     * 分页查询实际上利用的是limit语句，比例每页7条，显示第3页，则使用limit 14，7
     */
    @Override
    public PageInfo<Role> displayRolePage(RoleCondition roleCondition) {
        String sql = "select * from role";
        WhereSql where = WhereSql.buildAnd(roleCondition);
        int pageSize = roleCondition.getPageSize();
        int pageCount = roleCondition.getPageCount();
        String limit = " limit " + pageSize * (pageCount - 1) + "," + pageSize;
        sql = sql + where.getStringWhereSql() + limit;
        System.out.println(sql);
        List<Role> roleList = OsBeanUtils.resultSetToList(JDBCUtils.executeQuery(sql), Role.class);
        PageInfo<Role> pageInfo = new PageInfo<>();
        pageInfo.setList(roleList)
                .setPageSize(pageSize)
                .setPageCount(pageCount);
        return pageInfo;
    }

    @Override
    public List<Role> findChildren(Role role) {
        String sql = "select * from Role where parentId=" + role.getId();
        return OsBeanUtils.resultSetToList(JDBCUtils.executeQuery(sql), Role.class);
    }

    public List<Role> list(RoleCondition roleCondition) {
        String sql = "select * from role";
        WhereSql where = WhereSql.buildAnd(roleCondition);
        return OsBeanUtils.resultSetToList(JDBCUtils.executeQuery(sql, where), Role.class);
    }

    @Override
    public boolean modify(Role role) {
        String sql = JDBCUtils.getSql("role.update");
        Date date = new Date();
        int result = JDBCUtils.executeUpdate(sql, role.getName(), role.getParentId(), role.getLevel()
                , role.getState(), date, role.getId());
        return result > 0;
    }

    @Override
    public int updateRole(Role role) {
        String sql = JDBCUtils.getSql("role.update");
//        Date date = new Date();
//        return JDBCUtils.executeUpdate(sql, role.getName(), role.getLevel()
//                , role.getState(), date, role.getId());
        return DaoUtils.updateById(role);

    }
}

